﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A2.ERP.Common
{
    public static class CaptchHelper
    {
        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="length">随机个数</param>
        /// <returns></returns>
        public static string CreatRandom(int length)
        {
            return Guid.NewGuid().ToString("N").Substring(0,length);
        }

        public static byte[] DrawImage(string vcode,float fontsize=14,Color backgroud=default(Color),Color border = default(Color))
        {
            //随机转动的角度

            const int RandAngle = 45;

            //var height=(int)(fontsize+4)

            var width = vcode.Length * (int)fontsize;

            //创建图片背景
            using (var map=new Bitmap(width+3,(int)fontsize+10))
            {
                using (var graphics=Graphics.FromImage(map))
                {
                    graphics.Clear(backgroud);//清除画面，填充背景

                    graphics.DrawRectangle(new Pen(border, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框

                    var random = new Random();

                    //背景噪点生成
                    var blackpen = new Pen(Color.DarkGray,0);

                    for (var i = 0; i < 50; i++)
                    {
                        int x = random.Next(0, map.Width);
                        int y = random.Next(0, map.Height);

                        graphics.DrawRectangle(blackpen, x, y, 1, 1);
                    }

                    //验证码旋转
                    var chars = vcode.ToCharArray();

                    //文字居中
                    var format = new StringFormat(StringFormatFlags.NoClip)
                    {
                        Alignment=StringAlignment.Center,
                        LineAlignment=StringAlignment.Center
                    };
                    //定义颜色
                    Color[] colors = { Color.Black, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan };

                    FontStyle[] style = { FontStyle.Bold, FontStyle.Italic, FontStyle.Regular, FontStyle.Underline };

                    //定义字体
                    string[] fonts = { "Verdana","宋体","Arial","Comic Sans MS" };

                    foreach (char item in chars)
                    {
                        int clindex = random.Next(colors.Length);
                        int findex = random.Next(fonts.Length);
                        int sindex = random.Next(style.Length);

                        var font = new Font(fonts[findex], fontsize, style[sindex]);

                        Brush b = new SolidBrush(colors[clindex]);

                        var dot = new Point(16, 16);

                        //
                        float angle = random.Next(-RandAngle, RandAngle);//转动的度数
                        graphics.TranslateTransform(dot.X,dot.Y);//移动到指定位置

                        graphics.RotateTransform(angle);

                        graphics.DrawString(item.ToString(CultureInfo.InvariantCulture), font, b, 1, 1, format);

                        graphics.RotateTransform(-angle);//转回去

                        graphics.TranslateTransform(2, -dot.Y);//移动到指定位置
                    }
                }

                //生成图片
                var stream = new MemoryStream();

                map.Save(stream,ImageFormat.Gif);

                //输出图片流
                return stream.ToArray();
            }


        }
    }
}
